package com.ygqh.baby.controller.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgQrCodeService;
import com.ygqh.baby.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信相关接口
 *
 * @author guohao
 */
@Controller
@RequestMapping("/mobile/qrcode")
public class YgQrCodeController extends BaseController {

    @Autowired
    private YgQrCodeService ygQrCodeService;
    @Value("${promotion.link}")
    protected String promotionLink;

    @Value("${image.domain}")
    private String domain;

    @RequestMapping(value = "getQrCodeForInvite", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject getQrCodeForInvite(@RequestParam AppType appType, @RequestParam String inviteCode, String callback) throws Exception {

        YgUser user = SessionUtil.getCurrentUser();
        String url = promotionLink + "?sourceCode" + user.getSourceCode() + "&inviteCode=" + inviteCode;
        if (AppType.Small.equals(appType)) {
            url = "pages/index/index?" + "?sourceCode" + user.getSourceCode() + "&inviteCode=" + inviteCode;
        }

        String qrCodeForInvite = ygQrCodeService.getQrCodeForInvite(appType, inviteCode, url);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("codeUrl", qrCodeForInvite);
        return new JSONPObject(callback, Message.success(map));
    }

    @RequestMapping("getQrCodeUrl")
    @ResponseBody
    public JSONPObject getQrCodeUrl(@RequestParam String url, @RequestParam String callback) throws Exception {
        String qrCodeUrl = ygQrCodeService.getQrCodeUrl(url);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("codeUrl", qrCodeUrl);
        return new JSONPObject(callback, Message.success(map));
    }


}
